package com.lianliankan.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpHelper {
	private HttpResponse httpResponse = null;
	private HttpEntity httpEntity = null;
	private String baseUrl = "http://192.168.1.103:8080/";

	public String httpGetRank(List<NameValuePair> nameValuePairs, String Uri) {
		InputStream inputStream = null;
		String result = "";
		try {
			HttpEntity requestHttpEntity = new UrlEncodedFormEntity(
					nameValuePairs);

			HttpPost httpPost = new HttpPost(baseUrl + Uri);
			httpPost.setEntity(requestHttpEntity);
			if (!GlobalValue.cookie.equals("")) {
				httpPost.setHeader("Cookie", GlobalValue.cookie);
			}
			HttpClient httpClient = new DefaultHttpClient();

			httpResponse = httpClient.execute(httpPost);
			if (GlobalValue.cookie.equals("")) {
				String cookieFiled = httpResponse.getHeaders("Set-Cookie")[0]
						.getValue();
				GlobalValue.cookie = cookieFiled.split(";\\s*")[0];
			}
			httpEntity = httpResponse.getEntity();
			inputStream = httpEntity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					inputStream));

			String line = "";
			while ((line = reader.readLine()) != null) {
				result = result + line;
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

}
